{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from scipy import stats\n",
      "import numpy as np\n",
      "import zipline\n",
      "import pytz\n",
      "from datetime import datetime\n",
      "\n",
      "from pymc.distributions.timeseries import *\n",
      "from theano.tensor import repeat\n",
      "\n",
      "from scipy import optimize\n",
      "\n",
      "\n",
      "# set every possibility to be equally possible\n",
      "x_coin = np.linspace(0, 1, 101)\n",
      "\n",
      "\n",
      "with model_randomwalk:\n",
      "    # First optimize random walk\n",
      "    start = pm.find_MAP(vars=[alpha, beta], fmin=optimize.fmin_l_bfgs_b)\n",
      "    \n",
      "    # Sample\n",
      "    step = pm.NUTS(scaling=start)\n",
      "    trace_rw = pm.sample(2000, step, start=start, progressbar=False)\n",
      "    \n",
      "\n",
      "fig = plt.figure(figsize=(8, 6))\n",
      "ax = fig.add_subplot(111, xlabel='Price GDX in \\$', ylabel='Price GLD in \\$', \n",
      "            title='Posterior predictive regression lines')\n",
      "\n",
      "colors = np.linspace(0.1, 1, len(prices))\n",
      "colors_sc = np.linspace(0.1, 1, len(trace_rw[-500::10]['alpha'].T))\n",
      "mymap = plt.get_cmap('winter')\n",
      "mymap_sc = plt.get_cmap('winter')\n",
      "\n",
      "xi = np.linspace(prices.GDX.min(), prices.GDX.max(), 50)\n",
      "for i, (alpha, beta) in enumerate(zip(trace_rw[-500::10]['alpha'].T, trace_rw[-500::10]['beta'].T)):\n",
      "    for a, b in zip(alpha, beta):\n",
      "        ax.plot(xi, a + b*xi, alpha=.05, lw=1, c=mymap_sc(colors_sc[i]))\n",
      "        \n",
      "sc = ax.scatter(prices.GDX, prices.GLD, label='data', cmap=mymap, c=colors)\n",
      "cb = plt.colorbar(sc)\n",
      "cb.ax.set_yticklabels([str(p.date()) for p in prices[::len(prices)//10].index]);\n",
      "fig.savefig('ppc5.png')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "plt.legend?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}